package com.bbex.mapper;

import com.bbex.entity.MiningCoinVolume;
import com.bbex.entity.OfflineCoinVolume;
import com.bbex.entity.UserCoinVolume;
import com.bbex.sql.build.MiningCoinVolumeSqlBuild;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * 用户挖矿资产的操作信息；
 *
 * @author p
 */
@Mapper
public interface MiningCoinVolumeDao {

    @InsertProvider(type = MiningCoinVolumeSqlBuild.class, method = "insert")
    void insert(MiningCoinVolume miningCoinVolume);

    @SelectProvider(type = MiningCoinVolumeSqlBuild.class, method = "findById")
    MiningCoinVolume findById(String id);

    @UpdateProvider(type = MiningCoinVolumeSqlBuild.class, method = "updateById")
    long updateById(UserCoinVolume userCoinVolume);

    @Select("select id,user_id as userId,coin_id as coinId,coin_symbol as symbol,volume,advert_volume as advertVolume,lock_volume as lockVolume,bail_volume as bailVolume," +
            "otc_advert_volume as otcAdvertVolume,otc_lock_volume as otcLockVolume,create_by as createBy,update_by as updateBy,create_date as createDate," +
            "update_date as updateDate,version from js_plat_mining_coin_volume where user_id = #{userId}")
    List<MiningCoinVolume> findAll(String userId);

    @Select("select id,user_id as userId,coin_id as coinId,coin_symbol as symbol,volume,advert_volume as advertVolume,lock_volume as lockVolume,bail_volume as bailVolume," +
            "otc_advert_volume as otcAdvertVolume,otc_lock_volume as otcLockVolume,create_by as createBy,update_by as updateBy,create_date as createDate," +
            "update_date as updateDate,version from js_plat_mining_coin_volume where user_id = #{userId} and coin_id = #{coinId} limit 1")
    MiningCoinVolume findByUserIdAndCoinId(@Param("userId") String userId, @Param("coinId") String coinId);

    @Update("update js_plat_mining_coin_volume set volume = #{volume},version= version+1 where user_id = #{userId} and coin_id = #{coinId} and version= #{version}")
    long updateVolume(@Param("userId") String userId, @Param("coinId") String coinId, @Param("volume") BigDecimal addVolume, @Param("version") Long version);

    @Update("update js_plat_mining_coin_volume set volume = volume + #{addVolume},version = version + 1 where user_id = #{userId} and coin_id = #{coinId} and volume >= 0")
    long updateAddVolume(@Param("userId") String userId, @Param("coinId") String coinId, @Param("addVolume") BigDecimal addVolume);

}
